const app = getApp()
const request = (url, options) => {
  let Authorization = wx.getStorageSync('token')

  let serviceurl = `${app.globalData.serviceUrl}${url}`
  if (url.indexOf('app-api') > -1) {
    Authorization = wx.getStorageSync('intelligent_token')
    serviceurl = `${app.globalData.serviceUrl2}${url}`
  } else if (url.indexOf('traceability_platform_webservice') > -1) {
    Authorization = wx.getStorageSync('sytoken')
    serviceurl = `${app.globalData.serviceUrl3}${url}`
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: serviceurl,
      method: options.method,
      data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
      header: {
        Authorization: Authorization,
        sysUserId: wx.getStorageSync('sysUserId'),
        userInfoId: wx.getStorageSync('userInfoId'),
        userid: wx.getStorageSync('userid')
      },
      success(request) {
        if (request.data.code === 421 || request.data.code === 401) {
          // wx.reLaunch({
          //   url: '/pages/login/login',
          // })
        }
        else if (request.data.code === 200) {
          resolve(request.data)
        } else {
          wx.showToast({
            title: request.data.msg,
            icon: "none"
          })
          reject(request.data)
        }
      },
      fail(error) {
        reject(error.data)
      }
    })
  })
}
const request2 = (url, options) => {
  let Authorization = wx.getStorageSync('token')
  let serviceurl = `${app.globalData.serviceUrl}${url}`
  if (url.indexOf('app-api') > -1) {
    Authorization = wx.getStorageSync('intelligent_token')
    serviceurl = `${app.globalData.serviceUrl2}${url}`
  } else if (url.indexOf('traceability_platform_webservice') > -1) {
    Authorization = wx.getStorageSync('sytoken')
    serviceurl = `${app.globalData.serviceUrl3}${url}`
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: serviceurl,
      method: options.method,
      data: options.data,
      header: {
        Authorization: Authorization,
        villageId: wx.getStorageSync('villageId'),
        userid: wx.getStorageSync('userid'),
        sysUserId: wx.getStorageSync('sysUserId'),
        userInfoId: wx.getStorageSync('userInfoId'),
        'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
      },
      success(request) {
        if (request.data.code === 421 || request.data.code === 401) {
          // wx.reLaunch({
          //   url: '/pages/login/login',
          // })
        }
        else if (request.data.code === 200) {
          resolve(request.data)
        } else {
          wx.showToast({
            title: request.data.msg,
            icon: "none"
          })
          reject(request.data)
        }
      },
      fail(error) {
        reject(error.data)
      }
    })
  })
}
const request3 = (url, options) => {
  let Authorization = wx.getStorageSync('token')
  let serviceurl = `${app.globalData.serviceUrl}${url}`
  if (url.indexOf('app-api') > -1) {
    Authorization = wx.getStorageSync('intelligent_token')
    serviceurl = `${app.globalData.serviceUrl2}${url}`
  } else if (url.indexOf('traceability_platform_webservice') > -1) {
    Authorization = wx.getStorageSync('sytoken')
    serviceurl = `${app.globalData.serviceUrl3}${url}`
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: serviceurl,
      method: options.method,
      data: options.data,
      header: {
        Authorization: Authorization,
        villageId: wx.getStorageSync('villageId'),
        userid: wx.getStorageSync('userid'),
        sysUserId:wx.getStorageSync('sysUserId'),
        userInfoId:wx.getStorageSync('userInfoId'),
        'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
      },
      success(request) {
        if (request.data.code === 421 || request.data.code === 401) {
          // wx.reLaunch({
          //   url: '/pages/login/login',
          // })
        }
        else if (request.data.code === 200) {
          resolve(request.data)
        } else {
          wx.showToast({
            title: request.data.msg,
            icon: "none"
          })
          reject(request.data)
        }
      },
      fail(error) {
        reject(error.data)
      }
    })
  })
}
const get = (url, options = {}) => {
  return request(url, {
    method: 'GET',
    data: options
  })
}
const post = (url, options) => {
  return request(url, {
    method: 'POST',
    data: options
  })
}
const fpost = (url, options) => {
  return request2(url, {
    method: 'POST',
    data: options
  })
}
const put = (url, options) => {
  return request(url, {
    method: 'PUT',
    data: options
  })
}
const remove = (url, options) => {
  return request(url, {
    method: 'DELETE',
    data: options
  })
}
const del = (url, options) => {
  return request3(url, {
    method: 'DELETE',
    data: options
  })
}
module.exports = {
  get,
  fpost,
  del,
  post,
  put,
  remove
}